var form = document.querySelector('form');
form.onsubmit = function (event) {
    console.log('-----')
    // 阻止默认行为
    event.preventDefault();

    var password = document.querySelector('input[name=password]').value;
    var confirm = document.querySelector('input[name=confirm]').value;
    console.log(password);
    console.log(confirm);
    if (password != confirm) {
        alert('两次输入密码必须一样');
        return;
    }
    // 发请求，提交数据
    var name = document.querySelector('input[name=name]').value;
    var phone = document.querySelector('input[name=phone]').value;
    var age = document.querySelector('input[name=age]').value;
    var request = new XMLHttpRequest();

    request.onreadystatechange = function () {
        // 服务端发送数据结束了;  responseText:响应数据
        console.log(request.responseText);
        if (request.readyState == 4) {
            console.log(request.readyState);
            var data = JSON.parse(request.responseText);
            console.log(data);
            if (data.result == 1) {
                //**登录成功，跳转到主页;location:地址对象;href:设置地址，相对于当前html文件的地址
                // (../)返回上级;   ( ./ ): 当前路径下
                location.href = './index.html';
            } else {
                // 登录失败，显示失败原因
                alert(data.message);
            }
        }
    }

        var queryStr = '?name=' + name + '&password=' + password;
        if (phone) {
            queryStr += '&phone=' + phone;
        }
        if (age) {
            queryStr += '&age=' + age;
        }
        request.open('GET', 'register.php' + queryStr);
        request.send();




    }